{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ef732dcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "import copy\n",
    "import  traceback\n",
    "import shutil\n",
    "import random\n",
    "\n",
    "import numpy as np  # linear algebra\n",
    "import pydicom\n",
    "from pydicom.errors import InvalidDicomError\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n",
    "from pydicom.uid import UID\n",
    "from PIL import Image\n",
    "from tqdm import tqdm\n",
    "import openpyxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2540dfb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_scan(path):\n",
    "    slices = [] #slices = [pydicom.dcmread(path + '/' + s) for s in filter(lambda x: x.endswith('.dcm'), os.listdir(path))]\n",
    "    for s in os.listdir(path):\n",
    "        if os.path.isdir(os.path.join(path, s)): #if not s.endswith('.dcm'):\n",
    "            continue\n",
    "        sl = pydicom.dcmread(os.path.join(path, s), force=True)\n",
    "        try:\n",
    "            sl_p = sl.pixel_array\n",
    "        except (AttributeError, InvalidDicomError):\n",
    "            traceback.print_exc()\n",
    "            print(f'\\tDelete {os.path.join(path, s)}')\n",
    "            os.remove(os.path.join(path, s))\n",
    "        else:\n",
    "            slices.append(sl)\n",
    "    slices.sort(key=lambda x: float(x.InstanceNumber))\n",
    "    return slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "24fa9706",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-100 500\n"
     ]
    }
   ],
   "source": [
    "window_width, window_level = 600, 200\n",
    "lower_b, upper_b = window_level - window_width//2, window_level + window_width//2\n",
    "print(lower_b, upper_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89db945b",
   "metadata": {},
   "source": [
    "## 1.阴性数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a3c79a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打印哪个病例没有2\n",
    "def print_no_cta(input_dir):\n",
    "    print(f'**********{input_dir}')\n",
    "    no_cta_list = []\n",
    "    for patient in sorted(os.listdir(input_dir)):\n",
    "        patient_path = os.path.join(input_dir, patient)\n",
    "        if os.path.isfile(patient_path): continue\n",
    "        if '2' not in os.listdir(patient_path):\n",
    "            no_cta_list.append(patient_path)\n",
    "            print(patient_path, os.listdir(patient_path))\n",
    "    return no_cta_list\n",
    "            \n",
    "no_cta_list = []\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-9-17-negative'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-9-29-negative'))\n",
    "print(no_cta_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac67f414",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将某个scan重命名为2，如果thickness距离1的thickness相同则选择thickness小的重命名\n",
    "for patient in no_cta_list:\n",
    "    scans = os.listdir(patient)\n",
    "    if '1' not in scans:\n",
    "        print(patient, 'not have 1')\n",
    "        continue\n",
    "    if len(scans) == 2:\n",
    "        for scan in scans:\n",
    "            if scan != '1':\n",
    "                os.rename(os.path.join(patient, scan), os.path.join(patient, '2'))\n",
    "    else:\n",
    "        tk_list = []\n",
    "        for scan in scans:\n",
    "            for s in os.listdir(os.path.join(patient, scan)):\n",
    "                if os.path.isdir(os.path.join(patient, scan, s)) or not s.endswith('.dcm'):\n",
    "                    continue\n",
    "                sl = pydicom.dcmread(os.path.join(patient, scan, s))\n",
    "                try:\n",
    "                    sl_p = sl.pixel_array\n",
    "                except AttributeError:\n",
    "                    continue\n",
    "                else:\n",
    "                    if scan == '1':\n",
    "                        ct_thickness = sl.SliceThickness\n",
    "                    else:\n",
    "                        tk_list.append((sl.SliceThickness, scan))\n",
    "        min_dis, min_scan, min_tk = 10000, None, 10000\n",
    "        for tk, scan in tk_list:\n",
    "            dis = abs(tk-ct_thickness)\n",
    "            if dis < min_dis or (dis == min_dis and tk < min_tk):\n",
    "                min_dis, min_scan, min_tk = dis, scan, tk\n",
    "        print(patient, min_scan)\n",
    "        os.rename(os.path.join(patient, min_scan), os.path.join(patient, '2'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c3a31892",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing wangronglin-16-21-28-81****\n",
      "****Processing wangsonglin-19-24-27-86****\n",
      "****Processing wangwei-32-39-50-139****\n",
      "****Processing wangwuzhuang-28-33-41-98****\n",
      "****Processing wangxinhui-18-23-31-85****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/wangxinhui-18-23-31-85/2/2-0087.dcm\n",
      "****Processing wangyunxian-18-27-39-134****\n",
      "****Processing wangyuzhi-29-38-55-156****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/wangyuzhi-29-38-55-156/2/2-00ED.dcm\n",
      "****Processing weiliping-14-19-29-82****\n",
      "****Processing wenshuinong-24-33-55-141****\n",
      "****Processing wufenai-9-15-28-81****\n",
      "****Processing wugaoyou-12-17-24-81****\n",
      "****Processing wuguorong-30-39-51-140****\n",
      "****Processing wujianguo-14-20-29-89****\n",
      "****Processing wumingshi2021040501-17-22-30-81****\n",
      "****Processing wutianshun-12-17-26-80****\n",
      "****Processing wuyunfang-20-25-34-85****\n",
      "****Processing wuzili-13-19-28-72****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/wuzili-13-19-28-72/2/2-0065.dcm\n",
      "****Processing xiajinying-22-32-46-134****\n",
      "****Processing xiangzhaochong-15-20-31-78****\n",
      "****Processing xiaxianlin-5-9-15-71****\n",
      "****Processing xiaxinghua-17-22-33-89****\n",
      "****Processing xiegenjun-21-26-34-93****\n",
      "****Processing xiemeiying-21-29-41-127****\n",
      "****Processing xieshangyong-14-20-27-87****\n",
      "****Processing xiezhaoqin-17-21-29-81****\n",
      "****Processing xinyongsheng-13-18-26-86****\n",
      "****Processing xucaizhen-27-37-51-131****\n",
      "****Processing xudingguo-21-25-38-89****\n",
      "****Processing xuguangtai-16-21-36-91****\n",
      "****Processing xushengwu-31-41-60-150****\n",
      "****Processing xuxuee-22-30-45-123****\n",
      "****Processing xuyizhen-22-32-47-149****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/xuyizhen-22-32-47-149/2/2-03B9.dcm\n",
      "****Processing yananfen-29-39-56-146****\n",
      "****Processing yanchunhong-36-45-64-155****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/yanchunhong-36-45-64-155/2/2-0103.dcm\n",
      "****Processing yangqingyue-31-39-50-128****\n",
      "****Processing yangxiaohui-20-28-39-125****\n",
      "****Processing yangyuqi-34-44-60-157****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/yangyuqi-34-44-60-157/2/2-00FF.dcm\n",
      "****Processing yangzengrong-28-40-58-161****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/yangzengrong-28-40-58-161/2/2-011F.dcm\n",
      "****Processing yangzhengfu-18-22-32-75****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/yangzhengfu-18-22-32-75/2/2-0074.dcm\n",
      "****Processing yangzhengqian-16-21-26-78****\n",
      "****Processing yaohuiping-16-21-29-79****\n",
      "****Processing yaojingen-20-25-31-81****\n",
      "****Processing yegenxiang-11-17-30-80****\n",
      "****Processing yepusheng-13-18-27-79****\n",
      "****Processing yeronghua-21-32-49-149****\n",
      "****Processing yeshanhui-33-43-55-143****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/yeshanhui-33-43-55-143/2/2-0101.dcm\n",
      "****Processing yeweigen-17-21-29-84****\n",
      "****Processing yimuyun-15-19-28-75****\n",
      "****Processing yiqiang-16-21-30-78****\n",
      "****Processing yuaiyuan-13-18-29-84****\n",
      "****Processing yuanashui-27-38-57-141****\n",
      "****Processing yuanjuhe-11-16-27-76****\n",
      "****Processing yuankunfa-27-33-44-89****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/yuankunfa-27-33-44-89/2/2-010D.dcm\n",
      "****Processing yuanyongjin-12-18-27-81****\n",
      "****Processing yuguangyang-18-23-33-90****\n",
      "****Processing yujixin-32-41-62-143****\n",
      "****Processing yukang-17-22-27-82****\n",
      "****Processing yukang-27-36-46-135****\n",
      "****Processing yumengting-19-26-36-123****\n",
      "****Processing yupengjie-26-34-46-134****\n",
      "****Processing yushengfu-31-40-56-154****\n",
      "****Processing yuweiliang-29-35-47-104****\n",
      "****Processing yuxiaotu-20-25-36-90****\n",
      "****Processing zengchunsheng-23-30-41-128****\n",
      "****Processing zhangchao-13-18-29-81****\n",
      "****Processing zhangdanye-31-38-50-143****\n",
      "****Processing zhangduofeng-12-17-28-55****\n",
      "****Processing zhangfagen-22-27-37-92****\n",
      "****Processing zhangguofeng-21-27-35-90****\n",
      "****Processing zhanghanmin-13-19-27-76****\n",
      "****Processing zhanghuanquan-17-22-29-75****\n",
      "****Processing zhanglanxian-19-28-48-129****\n",
      "****Processing zhangmingfa-22-27-37-93****\n",
      "****Processing zhangqingrong-32-43-73-161****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhangqingrong-32-43-73-161/2/2-0085.dcm\n",
      "****Processing zhangxianjun-16-23-31-82****\n",
      "****Processing zhangyao-17-22-29-83****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhangyao-17-22-29-83/2/2-0109.dcm\n",
      "****Processing zhangyulin-20-25-40-93****\n",
      "****Processing zhaofuchang-16-21-30-80****\n",
      "****Processing zhaoshunshou-30-38-53-139****\n",
      "****Processing zhaoyouyang-24-34-41-125****\n",
      "****Processing zhengguanhan-17-23-34-79****\n",
      "****Processing zhengjilang-23-33-49-135****\n",
      "****Processing zhengronghua-21-30-44-127****\n",
      "****Processing zhengwanhua-29-40-59-156****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhengwanhua-29-40-59-156/2/2-03D1.dcm\n",
      "****Processing zhengwenyan-15-20-29-82****\n",
      "****Processing zhengyingu-14-18-26-76****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhengyingu-14-18-26-76/2/2-007A.dcm\n",
      "****Processing zhengzaidi-22-34-51-153****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhengzaidi-22-34-51-153/2/2-2.5mm-00EB.dcm\n",
      "****Processing zhensuzhen-20-25-34-90****\n",
      "****Processing zhongxiaoxiang-30-40-53-139****\n",
      "****Processing zhouguoming-20-25-30-82****\n",
      "zhouguoming-20-25-30-82 height not equal to width\n",
      "\n",
      "****Processing zhouhongqiong-31-42-54-147****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhouhongqiong-31-42-54-147/2/2-00ED.dcm\n",
      "****Processing zhoujun-12-16-24-77****\n",
      "****Processing zhoulijun-18-23-35-93****\n",
      "****Processing zhouwenming-40-48-67-147****\n",
      "****Processing zhouxianhang-13-17-26-78****\n",
      "****Processing zhouyixiao-22-28-40-83****\n",
      "****Processing zhouyongyan-21-33-51-136****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhouyongyan-21-33-51-136/2/2-0101.dcm\n",
      "****Processing zhouzhangnv-12-18-31-80****\n",
      "****Processing zhuguizhou-18-23-34-85****\n",
      "****Processing zhuguofang-15-20-32-85****\n",
      "****Processing zhujianfu-16-22-28-87****\n",
      "****Processing zhujianfu-30-42-62-154****\n",
      "****Processing zhukuanwen-15-21-29-83****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmp/ipykernel_61213/3938569242.py\", line 8, in load_scan\n",
      "    sl_p = sl.pixel_array\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 836, in __getattr__\n",
      "    return object.__getattribute__(self, name)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1882, in pixel_array\n",
      "    self.convert_pixel_data()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1444, in convert_pixel_data\n",
      "    self._convert_pixel_data_without_handler()\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1556, in _convert_pixel_data_without_handler\n",
      "    raise last_exception  # type: ignore[misc]\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1536, in _convert_pixel_data_without_handler\n",
      "    self._do_pixel_data_conversion(handler)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/dataset.py\", line 1563, in _do_pixel_data_conversion\n",
      "    arr = handler.get_pixeldata(self)\n",
      "  File \"/disk1/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/pixel_data_handlers/numpy_handler.py\", line 283, in get_pixeldata\n",
      "    raise AttributeError(\n",
      "AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tDelete /nfs3-p1/zsxm/dataset/2021-9-29-negative/zhukuanwen-15-21-29-83/2/2-0084.dcm\n",
      "****Processing zhuodepeng-9-14-23-78****\n",
      "****Processing zhuyanjie-20-25-35-89****\n"
     ]
    }
   ],
   "source": [
    "# 将2下的dcm文件根据窗宽窗位转化为png图片\n",
    "def generate_image(input_folder):\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)) or f'images_{lower_b}_{upper_b}' in os.listdir(os.path.join(input_folder, patient, '2')):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        for scan in os.listdir(os.path.join(input_folder, patient)):\n",
    "            if scan != '2':\n",
    "                continue\n",
    "            name = patient #name = patient.split('-')[0]\n",
    "            image_path = os.path.join(input_folder, patient, scan, f'images_{lower_b}_{upper_b}')\n",
    "            if os.path.exists(image_path):\n",
    "                shutil.rmtree(image_path)\n",
    "            os.mkdir(image_path)\n",
    "\n",
    "            ct = load_scan(os.path.join(input_folder, patient, scan))\n",
    "            print_flag = False\n",
    "            for i in range(len(ct)):\n",
    "                img = ct[i].pixel_array.astype(np.int16)\n",
    "                intercept = ct[i].RescaleIntercept\n",
    "                slope = ct[i].RescaleSlope\n",
    "                if slope != 1:\n",
    "                    img = (slope * img.astype(np.float64)).astype(np.int16)\n",
    "                img += np.int16(intercept)\n",
    "                img = np.clip(img, lower_b, upper_b)\n",
    "                img = ((img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "                img = Image.fromarray(img)\n",
    "                if img.height != img.width:\n",
    "                    if not print_flag:\n",
    "                        print(patient, 'height not equal to width\\n')\n",
    "                        print_flag = True\n",
    "                    height = width = min(img.height, img.width)\n",
    "                    if img.height != height:\n",
    "                        start = (img.height - height) / 2\n",
    "                        img = img.crop((0, start, img.width, start + height))\n",
    "                    elif img.width != width:\n",
    "                        start = (img.width - width) / 2\n",
    "                        img = img.crop((start, 0, start + height, img.height))\n",
    "                img.save(os.path.join(image_path, f'{name}_{i:04d}.png'))\n",
    "\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-29-negative/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eab39463",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将各个病例中的png图片文件夹统一移动到一起供yolov5检测\n",
    "def move_together_for_detect(input_folder, dst_path):\n",
    "    workbook_path = os.path.join(input_folder, 'label.xlsx')\n",
    "    wb = openpyxl.load_workbook(workbook_path)\n",
    "    sheet = wb['Sheet1']\n",
    "    \n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "    root_name = input_folder.split('/')[-1] if input_folder.split('/')[-1] != '' else input_folder.split('/')[-2]\n",
    "    dst_path = os.path.join(dst_path, root_name)\n",
    "\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        flag = True\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == patient.split('-')[0]:\n",
    "                if row[3].value is not None and row[4].value is not None:\n",
    "                    flag = False\n",
    "                break\n",
    "        else:\n",
    "            raise Exception(f'cant find {patient.split(\"-\")[0]} in label.xlsx')\n",
    "        if flag: continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        name = patient #name = patient.split('-')[0]\n",
    "        if os.path.exists(os.path.join(dst_path, name)):\n",
    "            print(f\"\\tremove {os.path.join(dst_path, name)}\")\n",
    "            shutil.rmtree(os.path.join(dst_path, name))\n",
    "\n",
    "        try:\n",
    "            shutil.copytree(os.path.join(input_folder, patient, '2', f'images_{lower_b}_{upper_b}'), os.path.join(dst_path, name))\n",
    "        except:\n",
    "            traceback.print_exc()\n",
    "\n",
    "move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-17-negative/', '/nfs3-p1/zsxm/dataset/9_detect/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd4bc87a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#将检测结果移动回原文件夹内\n",
    "def move_back(result_path, ori_path):\n",
    "    for patient in sorted(os.listdir(result_path)):\n",
    "        print(f'Processing {patient}')\n",
    "        p_res_path = os.path.join(result_path, patient)\n",
    "        o_res_path = os.path.join(ori_path, patient, '2', f'pred_images_{lower_b}_{upper_b}')\n",
    "        if os.path.exists(o_res_path):\n",
    "            shutil.rmtree(o_res_path)\n",
    "        os.mkdir(o_res_path)\n",
    "        for file in os.listdir(p_res_path):\n",
    "            if os.path.isfile(os.path.join(p_res_path, file)):\n",
    "                shutil.move(os.path.join(p_res_path, file), os.path.join(o_res_path, file))\n",
    "            elif os.path.isdir(os.path.join(p_res_path, file)):\n",
    "                if os.path.exists(os.path.join(ori_path, patient, file)):\n",
    "                    shutil.rmtree(os.path.join(ori_path, patient, file))\n",
    "                shutil.move(os.path.join(p_res_path, file), os.path.join(ori_path, patient, '2', file))\n",
    "        os.rmdir(p_res_path)\n",
    "    os.rmdir(result_path)\n",
    "                \n",
    "move_back('/home/zsxm/pythonWorkspace/yolov5_old/runs/detect/2021-9-17-negative', '/nfs3-p1/zsxm/dataset/2021-9-17-negative/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7674b5e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切出主动脉,这里有问题啊，branch_end前后0.3的切片切不出来，建议以后更改为和下面一样的方案\n",
    "def find_coordinate(height, width, label_file, aorta):\n",
    "    with open(label_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    assert len(lines) <= 2, f'label.txt应该存储不多于2个label：{label_file.split(\"/\")[-1]}'\n",
    "    if len(lines) == 1:\n",
    "        assert aorta == 'j', f'如果只有一个label那么此时应为降主动脉, 但实际为{aorta}：{label_file.split(\"/\")[-1]}'\n",
    "        corr = list(map(lambda x: float(x), lines[0].split()))\n",
    "        x, y, w, h = corr[1], corr[2], corr[3], corr[4]\n",
    "        assert 0.25 < x < 0.75 and 0.2 < y < 0.8, f'边界框中心({x}, {y})出界：{label_file.split(\"/\")[-1]}'\n",
    "    else:\n",
    "        corr1, corr2 = list(map(lambda x: float(x), lines[0].split())), list(map(lambda x: float(x), lines[1].split()))\n",
    "        assert 0.25 < corr1[1] < 0.75 and 0.2 < corr1[2] < 0.8, f'边界框1中心({corr1[1]}, {corr1[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        assert 0.25 < corr2[1] < 0.75 and 0.2 < corr2[2] < 0.8, f'边界框2中心({corr2[1]}, {corr2[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        if aorta == 's':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] < corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        elif aorta == 'j':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] > corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        else:\n",
    "            raise Exception(f'aorta 应该为\"s\"或\"j\"其中之一: {label_file.split(\"/\")[-1]}')\n",
    "    w, h = int(width*w), int(height*h)\n",
    "    w, h = max(w, h), max(w, h)\n",
    "    return int(width*x-w/2), int(height*y-h/2), int(width*x+w/2+1), int(height*y+h/2+1)\n",
    "\n",
    "def crop_images(input_path, error_patient_list):\n",
    "    workbook_path = os.path.join(input_path, 'label.xlsx')\n",
    "    wb = openpyxl.load_workbook(workbook_path)\n",
    "    sheet = wb['Sheet1']\n",
    "    \n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        if os.path.isfile(os.path.join(input_path, patient)):\n",
    "            continue\n",
    "        flag = True\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == patient.split('-')[0]:\n",
    "                if row[3].value is not None and row[4].value is not None:\n",
    "                    flag = False\n",
    "                    ls = row[4].value.split('-')\n",
    "                    assert len(ls) == 4, f'{patient} ls wrong'\n",
    "                    aorta_start, branch_start = int(ls[0])-1, int(ls[1])-1\n",
    "                    branch_end, aorta_end = int(ls[2])-1, int(ls[3])-1\n",
    "                    lsct = row[3].value.split('-')\n",
    "                    assert len(lsct) == 4, f'{patient} lsct wrong'\n",
    "                    ct_start, ct_end = int(lsct[0])-1, int(lsct[3])-1\n",
    "                break\n",
    "        if flag: continue\n",
    "        print(f'******Processing {patient}******')\n",
    "        image_path = os.path.join(input_path, patient, '2', f'images_{lower_b}_{upper_b}')\n",
    "        label_path = os.path.join(input_path, patient, '2', 'labels')\n",
    "        crop_path = os.path.join(input_path, patient, '2', f'crops_{lower_b}_{upper_b}')\n",
    "        if os.path.exists(crop_path):\n",
    "            shutil.rmtree(crop_path)\n",
    "        os.mkdir(crop_path)\n",
    "        \n",
    "        m, n = ct_end - ct_start, aorta_end - aorta_start\n",
    "        ot, q = [], 0\n",
    "        for p in range(m):\n",
    "            min_dis = abs(q/n-p/m)\n",
    "            while q < n:\n",
    "                q += 1\n",
    "                if abs(q/n-p/m) < min_dis:\n",
    "                    min_dis = abs(q/n-p/m)\n",
    "                else:\n",
    "                    q -= 1\n",
    "                    ot.append(q)\n",
    "                    break\n",
    "        assert len(ot) == m, f'{patient} ot wrong'\n",
    "        len_ct = len(os.listdir(os.path.join(input_path, patient, '1', f'images_{lower_b}_{upper_b}')))\n",
    "        len_cta = len(os.listdir(image_path))\n",
    "        cta_ct_table = [None] * len_cta\n",
    "        for i in range(len_ct):\n",
    "            idx = aorta_start + (i-ct_start)//m*n + ot[(i-ct_start)%m]\n",
    "            if idx < 0 or idx >= len_cta: continue\n",
    "            cta_ct_table[idx] = i\n",
    "        \n",
    "        crop_flag = True\n",
    "        offset = branch_end - branch_start\n",
    "        start, end = branch_start + int(0.1*offset), branch_end - int(0.2*offset)\n",
    "        for i in range(start, end):\n",
    "            img = Image.open(os.path.join(image_path, f'{patient}_{i:04d}.png'))\n",
    "            img = np.array(img)\n",
    "            try:\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 's')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                crop_name = f'{patient}_s_{i:04d}.png' if cta_ct_table[i] is None else f'{patient}_s_{i:04d}_{cta_ct_table[i]:04d}.png'\n",
    "                crop.save(os.path.join(crop_path, crop_name))\n",
    "            try:\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 'j')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                crop_name = f'{patient}_j_{i:04d}.png' if cta_ct_table[i] is None else f'{patient}_j_{i:04d}_{cta_ct_table[i]:04d}.png'\n",
    "                crop.save(os.path.join(crop_path, crop_name))\n",
    "        offset = aorta_end - branch_end\n",
    "        start, end = branch_end + int(0.1*offset), aorta_end - int(0.2*offset)\n",
    "        for i in range(start, end):\n",
    "            img = Image.open(os.path.join(image_path, f'{patient}_{i:04d}.png'))\n",
    "            img = np.array(img)\n",
    "            try:\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 'j')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                crop_name = f'{patient}_j_{i:04d}.png' if cta_ct_table[i] is None else f'{patient}_j_{i:04d}_{cta_ct_table[i]:04d}.png'\n",
    "                crop.save(os.path.join(crop_path, crop_name))\n",
    "        if not crop_flag:\n",
    "            #print('Delete crop_path')\n",
    "            #shutil.rmtree(crop_path)\n",
    "            error_patient_list.append(patient)\n",
    "            \n",
    "epl1 = []\n",
    "crop_images('/nfs3-p1/zsxm/dataset/2021-9-17-negative/', epl1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9085b36",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(epl1))\n",
    "print(epl1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eee3956",
   "metadata": {},
   "source": [
    "## 2.疾病数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ccefbbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打印哪个病例没有2\n",
    "def print_no_cta(input_dir):\n",
    "    print(f'**********{input_dir}')\n",
    "    no_cta_list = []\n",
    "    for patient in sorted(os.listdir(input_dir)):\n",
    "        patient_path = os.path.join(input_dir, patient)\n",
    "        if os.path.isfile(patient_path): continue\n",
    "        if '2' not in os.listdir(patient_path):\n",
    "            no_cta_list.append(patient_path)\n",
    "            print(patient_path, os.listdir(patient_path))\n",
    "    return no_cta_list\n",
    "\n",
    "no_cta_list = []\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p1/zsxm/dataset/2021-9-8'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p1/zsxm/dataset/2021-9-13/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p1/zsxm/dataset/2021-9-19/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p1/zsxm/dataset/2021-9-28/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-10-19-imh/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-10-19-pau/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-10-19-aa/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-11-20/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-11-20-imh/'))\n",
    "no_cta_list.extend(print_no_cta('/nfs3-p2/zsxm/dataset/2021-11-20-pau/'))\n",
    "print(no_cta_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cb8da35",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将某个scan重命名为2，如果thickness距离1的thickness相同则选择thickness小的重命名\n",
    "for patient in no_cta_list:\n",
    "    scans = os.listdir(patient)\n",
    "    if '1' not in scans:\n",
    "        print(patient, 'not have 1')\n",
    "        continue\n",
    "    if len(scans) == 2:\n",
    "        for scan in scans:\n",
    "            if scan != '1':\n",
    "                os.rename(os.path.join(patient, scan), os.path.join(patient, '2'))\n",
    "    else:\n",
    "        tk_list = []\n",
    "        for scan in scans:\n",
    "            for s in os.listdir(os.path.join(patient, scan)):\n",
    "                if os.path.isdir(os.path.join(patient, scan, s)) or not s.endswith('.dcm'):\n",
    "                    continue\n",
    "                sl = pydicom.dcmread(os.path.join(patient, scan, s))\n",
    "                try:\n",
    "                    sl_p = sl.pixel_array\n",
    "                except AttributeError:\n",
    "                    continue\n",
    "                else:\n",
    "                    if scan == '1':\n",
    "                        ct_thickness = sl.SliceThickness\n",
    "                    else:\n",
    "                        tk_list.append((sl.SliceThickness, scan))\n",
    "        min_dis, min_scan, min_tk = 10000, None, 10000\n",
    "        for tk, scan in tk_list:\n",
    "            dis = abs(tk-ct_thickness)\n",
    "            if dis < min_dis or (dis == min_dis and tk < min_tk):\n",
    "                min_dis, min_scan, min_tk = dis, scan, tk\n",
    "        print(patient, min_scan)\n",
    "        os.rename(os.path.join(patient, min_scan), os.path.join(patient, '2'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4c67ccd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing caiweiguang-J-Im35-152****\n",
      "****Processing chenbo-S-Im24-45-J-Im24-97****\n",
      "****Processing chenfujun-J-Im43-100****\n",
      "****Processing chenggang-J-Im18-81****\n",
      "****Processing chenjun-J-Im33-137****\n",
      "****Processing chenlili-S-Im18-24-J-Im18-74****\n",
      "chenlili-S-Im18-24-J-Im18-74 height not equal to width\n",
      "\n",
      "****Processing chenping-J-Im23-88****\n",
      "chenping-J-Im23-88 height not equal to width\n",
      "\n",
      "****Processing chiyanfei-J-Im20-83****\n",
      "chiyanfei-J-Im20-83 height not equal to width\n",
      "\n",
      "****Processing chugentang-J-Im19-80****\n",
      "chugentang-J-Im19-80 height not equal to width\n",
      "\n",
      "****Processing daizuokou-J-Im19-74****\n",
      "****Processing guojianfu-J-Im32-100****\n",
      "****Processing guquankang-J-Im18-69****\n",
      "guquankang-J-Im18-69 height not equal to width\n",
      "\n",
      "----------------------------------------------------------------------------\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '/nfs3-p1/zsxm/dataset/2021-9-19/label.xlsx'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_61213/3634952141.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     54\u001b[0m \u001b[0mgenerate_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/nfs3-p1/zsxm/dataset/2021-9-13/'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     55\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'----------------------------------------------------------------------------'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 56\u001b[0;31m \u001b[0mgenerate_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/nfs3-p1/zsxm/dataset/2021-9-19/'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     57\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'----------------------------------------------------------------------------'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     58\u001b[0m \u001b[0mgenerate_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/nfs3-p1/zsxm/dataset/2021-9-28/'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/tmp/ipykernel_61213/3634952141.py\u001b[0m in \u001b[0;36mgenerate_image\u001b[0;34m(input_folder)\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgenerate_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_folder\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mworkbook_path\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_folder\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'label.xlsx'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m     \u001b[0mwb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopenpyxl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload_workbook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mworkbook_path\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m     \u001b[0msheet\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Sheet1'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mpatient\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_folder\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/pytorch/lib/python3.8/site-packages/openpyxl/reader/excel.py\u001b[0m in \u001b[0;36mload_workbook\u001b[0;34m(filename, read_only, keep_vba, data_only, keep_links)\u001b[0m\n\u001b[1;32m    313\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    314\u001b[0m     \"\"\"\n\u001b[0;32m--> 315\u001b[0;31m     reader = ExcelReader(filename, read_only, keep_vba,\n\u001b[0m\u001b[1;32m    316\u001b[0m                         data_only, keep_links)\n\u001b[1;32m    317\u001b[0m     \u001b[0mreader\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/pytorch/lib/python3.8/site-packages/openpyxl/reader/excel.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, fn, read_only, keep_vba, data_only, keep_links)\u001b[0m\n\u001b[1;32m    122\u001b[0m     def __init__(self,  fn, read_only=False, keep_vba=KEEP_VBA,\n\u001b[1;32m    123\u001b[0m                   data_only=False, keep_links=True):\n\u001b[0;32m--> 124\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marchive\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_validate_archive\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    125\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalid_files\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marchive\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnamelist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    126\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_only\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread_only\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/pytorch/lib/python3.8/site-packages/openpyxl/reader/excel.py\u001b[0m in \u001b[0;36m_validate_archive\u001b[0;34m(filename)\u001b[0m\n\u001b[1;32m     94\u001b[0m             \u001b[0;32mraise\u001b[0m \u001b[0mInvalidFileException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     95\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 96\u001b[0;31m     \u001b[0marchive\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mZipFile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     97\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0marchive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     98\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/pytorch/lib/python3.8/zipfile.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)\u001b[0m\n\u001b[1;32m   1249\u001b[0m             \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1250\u001b[0m                 \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1251\u001b[0;31m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfile\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilemode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1252\u001b[0m                 \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1253\u001b[0m                     \u001b[0;32mif\u001b[0m \u001b[0mfilemode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodeDict\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/nfs3-p1/zsxm/dataset/2021-9-19/label.xlsx'"
     ]
    }
   ],
   "source": [
    "# 将2下的dcm文件根据窗宽窗位转化为png图片\n",
    "def generate_image(input_folder):\n",
    "    workbook_path = os.path.join(input_folder, 'label.xlsx')\n",
    "    wb = openpyxl.load_workbook(workbook_path)\n",
    "    sheet = wb['Sheet1']\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        flag = True\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == patient.split('-')[0]:\n",
    "                if row[3].value is not None and row[4].value is not None:\n",
    "                    flag = False\n",
    "                break\n",
    "        else:\n",
    "            #raise Exception(f'cant find {patient.split(\"-\")[0]} in label.xlsx')\n",
    "            print(f'cant find {patient.split(\"-\")[0]} in label.xlsx')\n",
    "        if flag: continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        for scan in os.listdir(os.path.join(input_folder, patient)):\n",
    "            if scan != '2':\n",
    "                continue\n",
    "            name = patient #name = patient.split('-')[0]\n",
    "            image_path = os.path.join(input_folder, patient, scan, f'images_{lower_b}_{upper_b}')\n",
    "            if os.path.exists(image_path):\n",
    "                shutil.rmtree(image_path)\n",
    "            os.mkdir(image_path)\n",
    "\n",
    "            ct = load_scan(os.path.join(input_folder, patient, scan))\n",
    "            print_flag = False\n",
    "            for i in range(len(ct)):\n",
    "                img = ct[i].pixel_array.astype(np.int16)\n",
    "                intercept = ct[i].RescaleIntercept\n",
    "                slope = ct[i].RescaleSlope\n",
    "                if slope != 1:\n",
    "                    img = (slope * img.astype(np.float64)).astype(np.int16)\n",
    "                img += np.int16(intercept)\n",
    "                img = np.clip(img, lower_b, upper_b)\n",
    "                img = ((img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "                img = Image.fromarray(img)\n",
    "                if img.height != img.width:\n",
    "                    if not print_flag:\n",
    "                        print(patient, 'height not equal to width\\n')\n",
    "                        print_flag = True\n",
    "                    height = width = min(img.height, img.width)\n",
    "                    if img.height != height:\n",
    "                        start = (img.height - height) / 2\n",
    "                        img = img.crop((0, start, img.width, start + height))\n",
    "                    elif img.width != width:\n",
    "                        start = (img.width - width) / 2\n",
    "                        img = img.crop((start, 0, start + height, img.height))\n",
    "                img.save(os.path.join(image_path, f'{name}_{i:04d}.png'))\n",
    "\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-13/')\n",
    "print('----------------------------------------------------------------------------')\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-19/')\n",
    "print('----------------------------------------------------------------------------')\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-28/')\n",
    "print('----------------------------------------------------------------------------')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-10-19-aa/')\n",
    "print('----------------------------------------------------------------------------')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-11-20/')\n",
    "print('----------------------------------------------------------------------------')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-11-20-imh/')\n",
    "print('----------------------------------------------------------------------------')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-11-20-pau/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0f361c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将各个病例中的png图片文件夹统一移动到一起供yolov5检测\n",
    "def move_together_for_detect(input_folder, dst_path):\n",
    "    workbook_path = os.path.join(input_folder, 'label.xlsx')\n",
    "    wb = openpyxl.load_workbook(workbook_path)\n",
    "    sheet = wb['Sheet1']\n",
    "    \n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "    root_name = input_folder.split('/')[-1] if input_folder.split('/')[-1] != '' else input_folder.split('/')[-2]\n",
    "    dst_path = os.path.join(dst_path, root_name)\n",
    "\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        flag = True\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == patient.split('-')[0]:\n",
    "                if row[3].value is not None and row[4].value is not None:\n",
    "                    flag = False\n",
    "                break\n",
    "        else:\n",
    "            raise Exception(f'cant find {patient.split(\"-\")[0]} in label.xlsx')\n",
    "        if flag: continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        name = patient #name = patient.split('-')[0]\n",
    "        if os.path.exists(os.path.join(dst_path, name)):\n",
    "            print(f\"\\tremove {os.path.join(dst_path, name)}\")\n",
    "            shutil.rmtree(os.path.join(dst_path, name))\n",
    "\n",
    "        try:\n",
    "            shutil.copytree(os.path.join(input_folder, patient, '2', f'images_{lower_b}_{upper_b}'), os.path.join(dst_path, name))\n",
    "        except:\n",
    "            traceback.print_exc()\n",
    "\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-8/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-13/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-10-19-pau/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-10-19-imh/', '/nfs3-p1/zsxm/dataset/9_detect/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70b19ce9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#将检测结果移动回原文件夹内\n",
    "def move_back(result_path, ori_path):\n",
    "    for patient in sorted(os.listdir(result_path)):\n",
    "        print(f'Processing {patient}')\n",
    "        p_res_path = os.path.join(result_path, patient)\n",
    "        o_res_path = os.path.join(ori_path, patient, '2', f'pred_images_{lower_b}_{upper_b}')\n",
    "        if os.path.exists(o_res_path):\n",
    "            shutil.rmtree(o_res_path)\n",
    "        os.mkdir(o_res_path)\n",
    "        for file in os.listdir(p_res_path):\n",
    "            if os.path.isfile(os.path.join(p_res_path, file)):\n",
    "                shutil.move(os.path.join(p_res_path, file), os.path.join(o_res_path, file))\n",
    "            elif os.path.isdir(os.path.join(p_res_path, file)):\n",
    "                if os.path.exists(os.path.join(ori_path, patient, file)):\n",
    "                    shutil.rmtree(os.path.join(ori_path, patient, file))\n",
    "                shutil.move(os.path.join(p_res_path, file), os.path.join(ori_path, patient, '2', file))\n",
    "        os.rmdir(p_res_path)\n",
    "    os.rmdir(result_path)\n",
    "                \n",
    "# move_back('/home/zsxm/pythonWorkspace/yolov5_old/runs/detect/2021-9-8', '/nfs3-p1/zsxm/dataset/2021-9-8/')\n",
    "# move_back('/home/zsxm/pythonWorkspace/yolov5_old/runs/detect/2021-9-13', '/nfs3-p2/zsxm/dataset/2021-9-13/')\n",
    "move_back('/home/zsxm/pythonWorkspace/yolov5_old/runs/detect/2021-10-19-pau', '/nfs3-p1/zsxm/dataset/2021-10-19-pau/')\n",
    "move_back('/home/zsxm/pythonWorkspace/yolov5_old/runs/detect/2021-10-19-imh', '/nfs3-p2/zsxm/dataset/2021-10-19-imh/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc7df67a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切出主动脉\n",
    "def find_coordinate(height, width, label_file, aorta):\n",
    "    with open(label_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    assert len(lines) <= 2, f'label.txt应该存储不多于2个label：{label_file.split(\"/\")[-1]}'\n",
    "    if len(lines) == 1:\n",
    "        assert aorta == 'j', f'如果只有一个label那么此时应为降主动脉, 但实际为{aorta}：{label_file.split(\"/\")[-1]}'\n",
    "        corr = list(map(lambda x: float(x), lines[0].split()))\n",
    "        x, y, w, h = corr[1], corr[2], corr[3], corr[4]\n",
    "        assert 0.25 < x < 0.75 and 0.15 < y < 0.85, f'边界框中心({x}, {y})出界：{label_file.split(\"/\")[-1]}'\n",
    "    else:\n",
    "        corr1, corr2 = list(map(lambda x: float(x), lines[0].split())), list(map(lambda x: float(x), lines[1].split()))\n",
    "        assert 0.25 < corr1[1] < 0.75 and 0.15 < corr1[2] < 0.85, f'边界框1中心({corr1[1]}, {corr1[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        assert 0.25 < corr2[1] < 0.75 and 0.15 < corr2[2] < 0.85, f'边界框2中心({corr2[1]}, {corr2[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        if aorta == 's':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] < corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        elif aorta == 'j':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] > corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        else:\n",
    "            raise Exception(f'aorta 应该为\"s\"或\"j\"其中之一: {label_file.split(\"/\")[-1]}')\n",
    "    w, h = int(width*w), int(height*h)\n",
    "    w, h = max(w, h), max(w, h)\n",
    "    return int(width*x-w/2), int(height*y-h/2), int(width*x+w/2+1), int(height*y+h/2+1)\n",
    "\n",
    "def crop_images(input_path, error_patient_list):\n",
    "    workbook_path = os.path.join(input_path, 'label.xlsx')\n",
    "    wb = openpyxl.load_workbook(workbook_path)\n",
    "    sheet = wb['Sheet1']\n",
    "    \n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        if os.path.isfile(os.path.join(input_path, patient)):\n",
    "            continue\n",
    "        flag = True\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == patient.split('-')[0]:\n",
    "                if row[3].value is not None and row[4].value is not None:\n",
    "                    flag = False\n",
    "                    pl = row[4].value.lower().split('-')\n",
    "                    plct = row[3].value.lower().split('-')\n",
    "                    assert len(pl) == len(plct), f'{input_path}:{patient}, {len(pl)}, {len(plct)}'\n",
    "                break\n",
    "        if flag: continue\n",
    "        print(f'******Processing {patient}******')\n",
    "        image_path = os.path.join(input_path, patient, '2', f'images_{lower_b}_{upper_b}')\n",
    "        label_path = os.path.join(input_path, patient, '2', 'labels')\n",
    "        crop_path = os.path.join(input_path, patient, '2', f'crops_{lower_b}_{upper_b}')\n",
    "        if os.path.exists(crop_path):\n",
    "            shutil.rmtree(crop_path)\n",
    "        os.mkdir(crop_path)\n",
    "        \n",
    "        ct_lbs, cta_lbs = [], []\n",
    "        for i in range(len(pl)):\n",
    "            if pl[i] != 's' and  pl[i] != 'j':\n",
    "                cta_lbs.append(int(pl[i])-1)\n",
    "                ct_lbs.append(int(plct[i])-1)\n",
    "        ct_start, ct_end, cta_start, cta_end = min(ct_lbs), max(ct_lbs), min(cta_lbs), max(cta_lbs)\n",
    "        m, n = ct_end - ct_start, cta_end - cta_start\n",
    "        ot, q = [], 0\n",
    "        for p in range(m):\n",
    "            min_dis = abs(q/n-p/m)\n",
    "            while q < n:\n",
    "                q += 1\n",
    "                if abs(q/n-p/m) < min_dis:\n",
    "                    min_dis = abs(q/n-p/m)\n",
    "                else:\n",
    "                    q -= 1\n",
    "                    ot.append(q)\n",
    "                    break\n",
    "        assert len(ot) == m, f'{patient} ot wrong'\n",
    "        len_ct = len(os.listdir(os.path.join(input_path, patient, '1', f'images_{lower_b}_{upper_b}')))\n",
    "        len_cta = len(os.listdir(image_path))\n",
    "        cta_ct_table = [None] * len_cta\n",
    "        for i in range(len_ct):\n",
    "            idx = cta_start + (i-ct_start)//m*n + ot[(i-ct_start)%m]\n",
    "            if idx < 0 or idx >= len_cta: continue\n",
    "            cta_ct_table[idx] = i\n",
    "        \n",
    "        crop_flag = True\n",
    "        for i, s in enumerate(pl):\n",
    "            if s != 's' and s != 'j':\n",
    "                continue\n",
    "            start, end = int(pl[i+1])-1, int(pl[i+2])\n",
    "            for j in range(start, end):\n",
    "                img = Image.open(os.path.join(image_path, f'{patient}_{j:04d}.png'))\n",
    "                img = np.array(img)\n",
    "                try:\n",
    "                    x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{j:04d}.txt'), s)\n",
    "                except:\n",
    "                    traceback.print_exc()\n",
    "                    crop_flag = False\n",
    "                else:#if crop_flag:\n",
    "                    crop = img[y1:y2, x1:x2]\n",
    "                    crop = Image.fromarray(crop)\n",
    "                    crop_name = f'{patient}_{s}_{j:04d}.png' if cta_ct_table[j] is None else f'{patient}_{s}_{j:04d}_{cta_ct_table[j]:04d}.png'\n",
    "                    crop.save(os.path.join(crop_path, crop_name))\n",
    "        if not crop_flag:\n",
    "            #print('Delete crop_path')\n",
    "            #shutil.rmtree(crop_path)\n",
    "            error_patient_list.append(patient)\n",
    "\n",
    "epl1 = []\n",
    "\n",
    "crop_images('/nfs3-p1/zsxm/dataset/2021-9-8/', epl1)\n",
    "crop_images('/nfs3-p1/zsxm/dataset/2021-9-13/', epl1)\n",
    "crop_images('/nfs3-p1/zsxm/dataset/2021-10-19-pau/', epl1)\n",
    "crop_images('/nfs3-p1/zsxm/dataset/2021-10-19-imh/', epl1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "737a045b",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(epl1))\n",
    "print(epl1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54475919",
   "metadata": {},
   "source": [
    "## 3.复制文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b52180d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ct_path = f'/nfs3-p2/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}'\n",
    "for dataset in ['train', 'val']:\n",
    "    if dataset == 'train':\n",
    "        dst_path = f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}'\n",
    "    else:\n",
    "        pass\n",
    "    dst_path = f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}/trainA' if dataset == 'train' else f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}/testA'\n",
    "    if os.path.exists(dst_path):\n",
    "        shutil.rmtree(dst_path)\n",
    "    os.makedirs(dst_path)\n",
    "    for cate in range(4):\n",
    "        ori_path = os.path.join(ct_path, dataset, str(cate))\n",
    "        for img in os.listdir(ori_path):\n",
    "            shutil.copy(os.path.join(ori_path, img), os.path.join(dst_path, img))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbd65765",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set = set()\n",
    "val_set = set()\n",
    "for img in os.listdir(f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}/trainA'):\n",
    "    train_set.add(img.split('_')[0])\n",
    "for img in os.listdir(f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}/testA'):\n",
    "    val_set.add(img.split('_')[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4f884b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(train_set), len(val_set))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8687d553",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def move_cta(input_path, train_set, val_set):\n",
    "    workbook_path = os.path.join(input_path, 'label.xlsx')\n",
    "    wb = openpyxl.load_workbook(workbook_path)\n",
    "    sheet = wb['Sheet1']\n",
    "    \n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        if os.path.isfile(os.path.join(input_path, patient)):\n",
    "            continue\n",
    "        flag = True\n",
    "        for row in sheet.iter_rows():\n",
    "            if row[0].value == patient.split('-')[0]:\n",
    "                if row[3].value is not None and row[4].value is not None:\n",
    "                    flag = False\n",
    "                break\n",
    "        if flag: continue\n",
    "        print(f'******Processing {patient}******')\n",
    "        if patient in train_set:\n",
    "            dst_path = f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}/trainB'\n",
    "        elif patient in val_set:\n",
    "            dst_path = f'/nfs3-p2/zsxm/dataset/gan_aorta_{lower_b}_{upper_b}/testB'\n",
    "        else:\n",
    "            raise Exception(f'{patient} neither in train_set nor in val_set')\n",
    "        os.makedirs(dst_path, exist_ok=True)\n",
    "        ori_path = os.path.join(input_path, patient, '2', f'crops_{lower_b}_{upper_b}')\n",
    "        for img in os.listdir(ori_path):\n",
    "            shutil.copy(os.path.join(ori_path, img), os.path.join(dst_path, img))\n",
    "            \n",
    "move_cta('/nfs3-p1/zsxm/dataset/2021-9-17-negative/', train_set, val_set)\n",
    "move_cta('/nfs3-p1/zsxm/dataset/2021-9-8/', train_set, val_set)\n",
    "move_cta('/nfs3-p1/zsxm/dataset/2021-9-13/', train_set, val_set)\n",
    "move_cta('/nfs3-p1/zsxm/dataset/2021-10-19-pau/', train_set, val_set)\n",
    "move_cta('/nfs3-p1/zsxm/dataset/2021-10-19-imh/', train_set, val_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1608574",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b114a715",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
